﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Diagnostics;
using Ras2ClassLib;

namespace Ras2Form
{
    public partial class HitListDlg : Form
    {
        private HitList m_hitlist;
        private Vinsaeldarlisti[] m_persarray = new Vinsaeldarlisti[100];
        private int m_nHitsCount = 0;
        private bool m_bNewList = false;

        public bool NewList
        {
            get { return m_bNewList;  }
            set { m_bNewList = value; }
        }

        public bool WeekList
        {
            get { return m_bWeekList.Checked; }
            set { m_bWeekList.Checked = value; }
        }
        
        public HitList HitList
        {
            get
            {
                m_hitlist.Name = m_txtName.Text;
                m_hitlist.DateBegins = m_dtDateBegins.Value;
                m_hitlist.DateEnds = m_dtDateEnds.Value;
                m_hitlist.Weeklist = m_bWeekList.Checked;
                return m_hitlist;
            }
            set
            {
                m_hitlist = value;
                if (m_hitlist.DateBegins == null)
                {
                    DateTime dt = new DateTime();
                    dt = DateTime.Now;
                    m_dtDateBegins.Value = dt;
                    if (m_bWeekList.Checked)
                        m_dtDateEnds.Value = dt.AddDays(7);
                    String str = String.Format("{0}.{1}.{2}", dt.Day, dt.Month, dt.Year);
                    m_txtName.Text = str;
                }
                else
                {
                    m_txtName.Text = m_hitlist.Name;
                    m_dtDateBegins.Value = m_hitlist.DateBegins.Value;
                    m_dtDateEnds.Value = m_hitlist.DateEnds.Value;
                    m_bWeekList.Checked = m_hitlist.Weeklist;
                }
            }
        }

        public HitListDlg()
        {
            InitializeComponent();
        }

        public class Vinsaeldarlisti
        {
            private string _NumerLags;
            private string _Lag;
            private string _Flytjandi;

            public Vinsaeldarlisti(string numerlags, string lag, string flytjandi)
            {
                _NumerLags = numerlags;
                _Lag       = lag;
                _Flytjandi = flytjandi;
            }

            public string NumerLags
            {
                get { return _NumerLags; }
                set { _NumerLags = value; }
            }

            public string Lag
            {
                get { return _Lag; }
                set { _Lag = value; }
            }

            public string Flytjandi
            {
                get { return _Flytjandi; }
                set { _Flytjandi = value; }
            }
        }

        private void OnLoad(object sender, EventArgs e)
        {
            CenterToParent();

            if (m_bNewList)
            {
                m_btnAcceptList.Enabled = true;
                m_btnAddRow.Enabled = false;
                m_btnOK.Text = "Nýskrá";
            }
            else
            {
                m_btnAcceptList.Enabled = false;
                m_btnOK.Text = "Í lagi";
            }

            if (!this.DesignMode)
            {
                HitListController controller = new HitListController();

                foreach (UIHitListEntry s in controller.GetSongsOnHitList(m_hitlist.ID))
                {
                    m_persarray[m_nHitsCount++] = new Vinsaeldarlisti(s.Position.ToString(), s.SongName, s.PerformerName);
                    if (m_nHitsCount >= 100) break;
                }
            }

            m_gvLagalisti.DataSource = m_persarray;
            m_gvLagalisti.ReadOnly = true;
            m_gvLagalisti.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
            m_gvLagalisti.MultiSelect = false;
        }

        private void OnAddRow(object sender, EventArgs e)
        {
            HitListAddDlg dlg = new HitListAddDlg();
            if (dlg.ShowDialog() == DialogResult.OK)
            {
                UISong song = dlg.Song;
                Debug.WriteLine(String.Format("{0} {1} {2} {3}", song.ID, song.Name, song.PerformerID, song.PerformerName));
                m_persarray[m_nHitsCount++] = new Vinsaeldarlisti(m_nHitsCount.ToString(), song.Name, song.PerformerName);

                HitListEntry entry = new HitListEntry();
                entry.SongID = song.ID;
                entry.HitListID = m_hitlist.ID;
                entry.Position = m_nHitsCount;
                HitListController controller = new HitListController();
                controller.AddHitListEntry(entry);

                m_gvLagalisti.Refresh();
                m_gvLagalisti.Update();
            }
        }

        private void OnAcceptList(object sender, EventArgs e)
        {
            if (m_bNewList)
            {
                HitList hitlist = this.HitList;
                HitListController controller = new HitListController();
                controller.AddHitList(hitlist);

                m_btnAcceptList.Enabled = false;
                m_btnAddRow.Enabled = true;
            }
        }
    }
}
